package com.cskaoyan._14network._03tcp;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @author Macro_Ray
 * @since 2024/3/25 23:12
 */
// TCP服务器端
public class Server {
    public static void main(String[] args) throws IOException {
        // 1.创建ServerSocket对象，绑定端口
        ServerSocket ss = new ServerSocket(10086);
        // 2.监听客户端连接，返回一个 Socket 对象
        Socket socket = ss.accept();

        // 3.获取输入流，读数据，并把数据显示在控制台上
        // 字节流读数据会出现中文乱码，改成字符流就可以了

        /*InputStream is = socket.getInputStream();
        InputStreamReader isr = new InputStreamReader(is);
        BufferedReader br = new BufferedReader(isr);*/

        BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));

        int b;
        while ((b = br.read())!=-1){
            System.out.print((char) b);
        }
        // 4.释放资源
        socket.close();
        ss.close();
    }
}
